package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.model.params.OuttaskApplyParam;
import cn.stylefeng.guns.modular.rms.model.params.OuttaskParam;
import cn.stylefeng.guns.modular.rms.model.result.OuttaskApplyResult;
import cn.stylefeng.guns.modular.rms.model.result.OuttaskResult;
import cn.stylefeng.guns.modular.rms.service.OuttaskApplyService;
import cn.stylefeng.guns.modular.rms.service.OuttaskService;
import cn.stylefeng.guns.modular.wms.model.params.CollectParam;
import cn.stylefeng.guns.modular.wms.model.result.CollectResult;
import cn.stylefeng.guns.modular.wms.service.CollectService;
import cn.stylefeng.guns.utils.MemberTokenUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 *收藏模块管理
 */
@Controller
@RequestMapping("/api/collect")
@Api(tags = "收藏模块管理")
@CrossOrigin
public class ApiCollectController extends BaseController {

    @Autowired
    private CollectService collectService;

    /**
     * 查询收藏列表
     */
    @ResponseBody
    @RequestMapping(value = "/getCollectList",method = RequestMethod.GET)
    @ApiOperation(value = "查询收藏列表", notes = "查询收藏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getCollectList(CollectParam param) {
        int memberId=MemberTokenUtil.getMemberId();
        param.setMemberId(memberId);
        LayuiPageInfo pageInfo= collectService.findPageBySpec(param);
        return ResponseData.success(pageInfo);
    }

    /**
     * 查询收藏详情
     */
    @ResponseBody
    @RequestMapping(value = "/getCollectDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询收藏详情", notes = "查询收藏详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query")
    })
    public ResponseData getCollectDetail(Long id) {
        CollectParam param1=new CollectParam();
        param1.setId(id);
        CollectResult result=collectService.findBySpec(param1);
        return ResponseData.success(result);
    }

    /**
     * 添加/更新收藏
     */
    @ResponseBody
    @RequestMapping(value = "/addOrUpdateCollect",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新收藏", notes = "添加/更新收藏")
    public ResponseData addOrUpdateCollect(@RequestBody CollectParam param) {
        try {
            int memberId=MemberTokenUtil.getMemberId();
            if (param.getType()==1){//企业端浏览记录
                param.setComMemberId(memberId);
            }else{
                param.setMemberId(memberId);
            }
            collectService.update(param);
            return ResponseData.success();
        }catch (Exception e){
            return ResponseData.error(e.getMessage());
        }
    }

    /**
     * 删除收藏
     */
    @ResponseBody
    @RequestMapping(value = "/deleteCollect",method = RequestMethod.POST)
    @ApiOperation(value = "删除收藏", notes = "删除收藏")
    public ResponseData deleteCollect(@RequestBody CollectParam param) {
        try {
            collectService.removeById(param.getId());
            return ResponseData.success();
        }catch (Exception e){
            return ResponseData.error(e.getMessage());
        }
    }

}



